let { newcreate,score } = require('../models');
const auth = require('../untils/auth');

var createUser_fn = async (ctx, next) => {
    var username = ctx.session.username;

    var obj = {
        //用户
        username: username
    };
    ctx.render("newCreate.html", obj);


}
//创建调查问卷判断
var newCreate_fn = async (ctx, next) => {
    //创建人Id
    var usernameId = ctx.session.userId;
    //问卷标题
    var title = ctx.request.body.title;
    //问卷简介
    var remark = ctx.request.body.remark;
    //题目
    var topicOne = ctx.request.body.topicOne;
    var topicTwo = ctx.request.body.topicTwo;
    var topicThree = ctx.request.body.topicThree;
    var topicFour = ctx.request.body.topicFour;
    var topicFive = ctx.request.body.topicFive;
    var topicSix = ctx.request.body.topicSix;
    var topicSeven = ctx.request.body.topicSeven;
    var topicEight = ctx.request.body.topicEight;
    var topicNine = ctx.request.body.topicNine;
    var topicTen = ctx.request.body.topicTen.trim();

    await newcreate.create({
        // 发表调查问卷人Id
        CreaterUserId: usernameId,
        // 调查问卷名
        CreateTilte: title,
        // 调查简介
        remark: remark,
        //  题目
        topicOne: topicOne,
        topicTwo: topicTwo,
        topicThree: topicThree,
        topicFour: topicFour,
        topicFive: topicFive,
        topicSix: topicSix,
        topicSeven: topicSeven,
        topicEight: topicEight,
        topicNine: topicNine,
        topicTen: topicTen
    });
    let res = "";
    res = { code: 200, msg: "发表调查问卷成功" };
    ctx.body = JSON.stringify(res);
}


//获取数据库所有的问卷信息
let qs_fn=async(ctx,next)=>{
    var questionnairesAll = JSON.parse(JSON.stringify(await newcreate.findAll({
        order: [
            ['id', 'desc']
        ]
    })))

    console.log(questionnairesAll);
    
    var obj = {
        questionnairesAll,
    }

    
    ctx.render('questionnaire.html', obj)
}

//评分
let toScore_fn=async(ctx,next)=>{

    //问卷Id
    var qsid = ctx.query.id;
    // console.log(qsid);

    var currentQS = JSON.parse(JSON.stringify(await newcreate.findAll({
        where:{
            id:qsid
        }
    })))

    
    let obj={
        currentQS,
        qsid
    }
    ctx.render('score.html',obj)
}
//评分结果
let scoreResult_fn=async(ctx,next)=>{
    //评分人Id
    let scoreId=ctx.session.userId;
    
    //问卷id
    var qsid = ctx.request.body.qsId;
    //获取评分结果
    var scoreOne=ctx.request.body.scoreOne;
    var scoreTwo=ctx.request.body.scoreTwo;
    var scoreThree=ctx.request.body.scoreThree;
    var scoreFour=ctx.request.body.scoreFour;
    var scoreFive=ctx.request.body.scoreFive;
    var scoreSix=ctx.request.body.scoreSix;
    var scoreSeven=ctx.request.body.scoreSeven;
    var scoreEight=ctx.request.body.scoreEight;
    var scoreNine=ctx.request.body.scoreNine;
    var scoreTen=ctx.request.body.scoreTen;
    console.log(scoreOne);
        // 向score评分表存分数
    await score.create({
        qsId: qsid,
        scoreId: scoreId,
        scoreOne:scoreOne,
        scoreTwo:scoreTwo,
        scoreThree:scoreThree,
        scoreFour:scoreFour,
        scoreFive:scoreFive,
        scoreSix:scoreSix,
        scoreSeven:scoreSeven,
        scoreEight:scoreEight,
        scoreNine:scoreNine,
        scoreTen:scoreTen
        
        }), {
          where: {
            scoreId: scoreId,
            qsId: qsid,
          }};
        let res = "";
        res = { code: 200, msg: "参与调查问卷成功" };
        ctx.body = JSON.stringify(res);
}

module.exports = {
    '/createUser': ['get', createUser_fn],
    '/index/newCreate': ['post', newCreate_fn],
    '/index/goQuestionnaire':['get',auth,qs_fn],
    '/goScore':['get',auth,toScore_fn],
    '/index/scoreResult': ['post', scoreResult_fn],
}